package com.honey.mybatis.generator.model;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Decoration;
import com.honey.compilation.java.EnumField;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.EnumType;
import com.honey.core.dbmapping.structure.EnumType.EnumValue;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMybatis;
import com.honey.mybatis.generator.MybatisPlugin;

public class StateEnum extends AbstractMybatis {

	private EnumType enumType =null;
	public StateEnum(MybatisPlugin belongPlugin) {
		super(belongPlugin);
	}

	public StateEnum(MybatisPlugin belongPlugin,EnumType enumType) {
		super(belongPlugin);
		this.enumType = enumType;
	}
	

	public AbstractJavaCompilation createAbstractJavaCompilation(
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		InnerEnum answer = null;
		try {
			FullyQualifiedJavaType type = super.getStateEnumType(introspectSchema.getSchema(),enumType);
			String name = type.getShortName();
			answer = new InnerEnum(type);
			answer.addClassComment(enumType.getComment());
			List<EnumValue>  list = enumType.getEnums();
			for( EnumValue ev : list){
				EnumField ef = new EnumField(ev.getJavaProperty() );
				ef.addJavaDocLine(ev.getComment());
				Parameter p = new Parameter(enumType.getFullyQualifiedJavaType() ,ModelConstant.FIELD_VALUE);
				ef.addParameter(p , ev.getValue());
				
				p = new Parameter(JDKFullyQualifiedJavaType.getStringInstance() ,ModelConstant.FIELD_SHOW);
				ef.addParameter(p , ev.getComment());
				answer.addEnumFields(ef);
			}
			
			Field field = new Field(ModelConstant.FIELD_VALUE,enumType.getFullyQualifiedJavaType(), new Decoration(JavaVisibility.PRIVATE,false,true),"枚举值");
			field.setUseDefaultValue(false);
			answer.addField(field);
			field = new Field(ModelConstant.FIELD_SHOW,JDKFullyQualifiedJavaType.getStringInstance(), new Decoration(JavaVisibility.PRIVATE,false,true),"枚举名称");
			answer.addField(field);
			
			Method method = new Method(name, new MethodDecoration(JavaVisibility.PRIVATE,false ,false, true,false ));
			method.addJavaDocLine("构造方法");
			Parameter parameter = new Parameter(enumType.getFullyQualifiedJavaType(),ModelConstant.FIELD_VALUE);
			method.addParameter(parameter);
			parameter = new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),ModelConstant.FIELD_SHOW);
			method.addParameter(parameter).addBodyLine("this."+ModelConstant.FIELD_VALUE+" = "+ModelConstant.FIELD_VALUE).addBodyLine("this."+ModelConstant.FIELD_SHOW+" = "+ModelConstant.FIELD_SHOW);
			answer.addMethod(method );
			
			method = new Method("getInstance",type,  new MethodDecoration(JavaVisibility.PUBLIC,true ,true ),"通过枚举值获取枚举对象");
			parameter = new Parameter(enumType.getFullyQualifiedJavaType(),ModelConstant.FIELD_VALUE);
			parameter.addJavaDocLine("获取枚举对象的值");
			method.addParameter(parameter);
			method.addBodyLine(name+" []enums = "+name+".values()");
			if( enumType.getFullyQualifiedJavaType().isPrimitive() ){
				method.addBodyLine("for( "+name+" e : enums ) if (e."+ModelConstant.FIELD_VALUE+" == value)return e ;");
			}else{
				method.addBodyLine("for( "+name+" e : enums ) if (e."+ModelConstant.FIELD_VALUE+".equals( value ))return e ;");
			}
			method.addBodyLine("throw new RuntimeException( value + \"在"+name+"对象中是一个未知的枚举值. \") ;");
			answer.addMethod(method );
			
			method = new Method("getValue", new MethodDecoration(JavaVisibility.PUBLIC,false ,true ));
			method.setReturnType(enumType.getFullyQualifiedJavaType()).addBodyLine("return this."+ModelConstant.FIELD_VALUE);
			method.addJavaDocLine("获取枚举值(表示值)");
			answer.addMethod(method );
			method = new Method("getShow", new MethodDecoration(JavaVisibility.PUBLIC,false ,true )).setReturnType(JDKFullyQualifiedJavaType.getStringInstance()).addBodyLine("return this."+ModelConstant.FIELD_SHOW);
			method.addJavaDocLine("获取枚举显示名称");
			answer.addMethod(method );
			
			method = new Method("toString", new MethodDecoration(JavaVisibility.PUBLIC,false ,true )).setReturnType(JDKFullyQualifiedJavaType.getStringInstance()).addBodyLine("return String.valueOf(this."+ModelConstant.FIELD_VALUE+")");
			method.addJavaDocLine("to string");
			method.addAnnotation("@Override");
			answer.addMethod(method );
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer ;
	}	
}
